<template>
	<view>
		<cu-custom :isBack="true">
			<block slot="backText">返回</block>
			<block slot="content">{{navTitle}}</block>
		</cu-custom>
		<mescroll-body ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback" :up="upOption">
			<view v-for="(item,index) in dataList" :key="index">
				<circle-item @choose="commitChoose(item)" :circle="item" :chooseMode="mode=='choose'"></circle-item>
			</view>
		</mescroll-body>
	</view>
</template>

<script>
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import CircleItem from '@/components/circle-item.vue'
	import config from '@/api/config.js'
	import {
		getMyCircleList,
		getAllCircleBySort
	} from '@/api/api.js'
	export default {
		components: {
			CircleItem
		},
		mixins: [MescrollMixin],
		data() {
			return {
				mode:'my',
				dataList: [],
				upOption: {
					page: {
						size: 10 // 每页数据的数量,默认10
					},
					textNoMore: config.customNoMore,
					noMoreSize: 1, // 配置列表的总数量要大于等于5条才显示'-- END --'的提示
					empty: {
						tip: '您还没有加入任何圈子'
					}
				},
			}
		},
		computed: {
			navTitle() {
				 if(this.mode == 'my') {
					 return '我的圈子'
				 }
				 if(this.mode == 'choose') {
					 return '选择圈子'
				 }
				 if(this.mode == 'all') {
					 return '热门圈子'
				 }
			}
		},
		onLoad(options) {
			if(options.mode !== null && options.mode !== undefined) {
				this.mode = options.mode
			}
		},
		methods: {
			commitChoose(circle) 
			{
				const eventChannel = this.getOpenerEventChannel()
				eventChannel.emit('finishChoose', circle);
				uni.navigateBack({
					delta:1
				})
			},
			getDataList(pageIndex, pageSize) {
				if(this.mode == 'all') {
					getAllCircleBySort(pageIndex, pageSize).then(res => {
						let pageList = res.data.list
						if (pageIndex == 0) {
							this.dataList = pageList
						} else {
							this.dataList = this.dataList.concat(pageList)
						}
						let hasNext = pageList.length >= pageSize
						if (this.mescroll) {
							this.mescroll.endSuccess(this.dataList.length, hasNext);
						}
					}).catch(error => {
						if (this.mescroll) {
							this.mescroll.endErr()
						}
					})
					return 
				}
				getMyCircleList(pageIndex, pageSize).then(res => {
					let pageList = res.data.list
					if (pageIndex == 0) {
						this.dataList = pageList
					} else {
						this.dataList = this.dataList.concat(pageList)
					}
					let hasNext = pageList.length >= pageSize
					if (this.mescroll) {
						this.mescroll.endSuccess(this.dataList.length, hasNext);
					}
				}).catch(error => {
					if (this.mescroll) {
						this.mescroll.endErr()
					}
				})
			},
			downCallback() {
				if (this.mescroll) {
					this.mescroll.resetUpScroll()
				}
			},
			upCallback(page) {
				let pageIndex = page.num - 1
				this.getDataList(pageIndex, page.size)
			}
		},
	}
</script>

<style scoped>

</style>